import datetime
import json
import random

from flask import Blueprint
from RootPath import RootPath
import os
from common.YmlUtil import YmlUtil
from common.contactStr import sqlContact, mqttMessageContact
from common.mqttUtil import on_mqtt_connect, publishMqttData
from common.mysqlUtil import exc_sql
generPr = Blueprint("deviceGenerator", __name__)
path = os.path.join(RootPath.getRootPath(), "Application.yml")
MQTTconf = YmlUtil().readYml(path,"mqtt_test_cs")
MySQLconf = YmlUtil().readYml(path,"mysql_test_136")
deviceID="0412170"

class changshaData():
    def exc(self,):
        print(" ======cs======")
        point_value = {"time":( datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")}
        print(point_value)
        MFa01 ={"name":"0412170_MFa00","value":"104"}
        MFb01 ={"name":"0412170_MFb00","value":"163"}
        EEa01 =  {"name":"0412170_MFa00","value":"2"}
        EEb01 ={"name":"0412170_MFa00","value":"14835"}
        EE01 ={"name":"0412170_EE01","value":"3001"}

        data ={"Data":[EE01]}
        print(data)
        # {"time":"2020-11-20 9:19:31","Data":[{"name":"04121701_EEa01","value":"21043"},{"name":"04121701_EEb01","value":"2"}]}
        point_value.update(data)
        print(point_value)
        # sqldata=sqlContact(deviceID,point_value)
        # mqttdata=json.dumps(mqttMessageContact(deviceID,point_value))
        # print(sqldata)
        # print(mqttdata)
        on_mqtt_connect(MQTTconf)
        publishMqttData(MQTTconf,json.dumps(point_value))#推送mqtt消息要用json dump方法


# changshaData().exc()